package weaver.formmode.customjavacode.modeexpand.hitech;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.customjavacode.modeexpand.hitech.utils.HtModelUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-06-13
 * @Description: 科目order关系对应表
 * @ModifyLog:
 **/

public class KmOrderModeExpand extends AbstractModeExpandJavaCodeNew {
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User) param.get( "user" );
            //数据id
            int billid = -1;
            //模块id
            int modeid = -1;
            //模块id
            int userid = -1;
            RequestInfo requestInfo = (RequestInfo) param.get( "RequestInfo" );
            if (requestInfo != null) {
                billid = Util.getIntValue( requestInfo.getRequestid() );
                modeid = Util.getIntValue( requestInfo.getWorkflowid() );
                userid = Util.getIntValue( user.getUID() );
                if (billid > 0 && modeid > 0) {
                    /*
                     *@Description:
                     */
                    RecordSet rs = new RecordSet();
                    rs.writeLog( "kmOrderModeExpand-----------------" + billid );
                    //插入主表
                    String sql = "select kmsyorder,syks from uf_kmordergxb where id=?";
                    rs.executeQuery( sql, billid );
                    rs.executeUpdate( "delete from uf_ksordergxb where dataid=?",billid );
                    while (rs.next()) {
                        Map<String, String> dataMap = new HashMap<>();
                        String kmsyorder = Util.null2String( rs.getString( "kmsyorder" ) );
                        String syks = Util.null2String( rs.getString( "syks" ) );
                        String[] strs = kmsyorder.split( "," );
                        for (String orderid : strs) {
                            String AUFNR = HtModelUtil.getOrders( orderid ).get( "AUFNR" );
                            String GSBER = HtModelUtil.getOrders( orderid ).get( "GSBER" );
                            String KTEXT = HtModelUtil.getOrders( orderid ).get( "KTEXT" );
                            String USER6 = HtModelUtil.getOrders( orderid ).get( "USER6" );
                            List<Map<String, String>> resultList = HtModelUtil.getKsByOrder( USER6, syks );
                            for (Map<String, String> map : resultList) {
                                dataMap.put( "kmsyorder", orderid );
                                dataMap.put( "orderdyks", map.get( "ksid" ) );
                                dataMap.put( "orderdyksmc",map.get( "departmentname" ));
                                dataMap.put( "orderdyksbm", map.get( "departmentcode" ));
                                dataMap.put( "orders", AUFNR );
                                dataMap.put( "ordersname", KTEXT);
                                dataMap.put( "subcompanyid", GSBER);
                                dataMap.put( "supdepid", map.get( "supdepid" ));
                                dataMap.put( "dataid", billid+"");
                                HtModelUtil.insertModelData( 156, userid, dataMap );
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            result.put( "errmsg", "自定义出错信息" );
            result.put( "flag", "false" );
        }
        return result;
    }

}
